package me.maiz.babyisland.service.impl;

import me.maiz.babyisland.dao.model.Category;
import me.maiz.babyisland.dao.CategoryMapper;
import me.maiz.babyisland.service.ICategoryService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import me.maiz.babyisland.service.info.CategoryInfo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author lucas
 * @since 2019-05-28
 */
@Service
@Slf4j
public class CategoryServiceImpl extends ServiceImpl<CategoryMapper, Category> implements ICategoryService {


    @Override
    @Cacheable("categoies")
    public List<CategoryInfo> getAllCategoryInfo() {
        List<CategoryInfo> categoryInfos = new ArrayList<>();
        HashMap<String, Object> columnMap = new HashMap<>();
        //查找1级分类，特点是父id为-1
        columnMap.put("parent_id",-1);
        Collection<Category> categoriesLevelOne = listByMap(columnMap);

        //遍历1级分类
        for (Category category : categoriesLevelOne) {
            CategoryInfo c = new CategoryInfo();
            c.setCategoryId(category.getCategoryId());
            c.setName(category.getName());

            //父id为当前一级分类的id
            columnMap.put("parent_id",category.getCategoryId());
            Collection<Category> categoriesLevelTwo = listByMap(columnMap);

            c.setSubCategories(categoriesLevelTwo);
            categoryInfos.add(c);
        }

        log.info("分类：{}",categoryInfos);

        return categoryInfos;
    }
}
